cmake_minimum_required(VERSION 3.25.0)

# Project information setting.
project(app LANGUAGES C CXX ASM)

include_directories(${CMAKE_SOURCE_DIR})
include_directories(lvgl)
include_directories(lv_drivers)
include_directories(porting)
include_directories(porting/SDL2/include)

add_definitions(-DUSE_SDL)

# Project files path setting.
set(PRO_INC_FILE_FOLDERS
    "${CMAKE_SOURCE_DIR}/lv_drivers"
    "${CMAKE_SOURCE_DIR}/lvgl"
    "${CMAKE_SOURCE_DIR}/porting"
)
set(PRO_SRC_FILE_FOLDERS
    "${CMAKE_SOURCE_DIR}/porting"
)

# Find header files.
set(ProIncDirList "")
foreach(FolderItem ${PRO_INC_FILE_FOLDERS})
    list(APPEND ProIncDirList ${FolderItem})
    file(GLOB_RECURSE ListItems LIST_DIRECTORIES true ${FolderItem}/*)
    foreach(Item ${ListItems})
        if(IS_DIRECTORY ${Item})
            set(HaveFiles "")
            # If has header file, add path into ProIncDirList.
            file(GLOB HaveFiles ${Item}/*.h)
            if(HaveFiles)
                list(APPEND ProIncDirList ${Item})
            endif(HaveFiles)
        endif(IS_DIRECTORY ${Item})
    endforeach(Item ${ListItems})
endforeach(FolderItem ${PRO_INC_FILE_FOLDERS})

# Include header files directory list.
include_directories(${ProIncDirList})

# Find source files.
set(ProSrcFileList "")
foreach(FolderItem ${PRO_SRC_FILE_FOLDERS})
    aux_source_directory(${FolderItem} ProSrcFileList)
    file(GLOB_RECURSE ListItems LIST_DIRECTORIES true ${FolderItem}/*)
    foreach(Item ${ListItems})
        if(IS_DIRECTORY ${Item})
            # Add all source files under current folder.
            aux_source_directory(${Item} ProSrcFileList)
        endif(IS_DIRECTORY ${Item})
    endforeach(Item ${ListItems})
endforeach(FolderItem ${PRO_SRC_FILE_FOLDERS})

# Set executable target.
list(APPEND ProSrcFileList "main.c")
add_executable(${PROJECT_NAME} ${ProSrcFileList})

# Add test items.
add_subdirectory("lv_drivers")

target_link_libraries(${PROJECT_NAME}
    PRIVATE
    "${CMAKE_SOURCE_DIR}/lvgl/liblvgl.a"
    "${CMAKE_BINARY_DIR}/lib/liblv_drivers.a"
    "${CMAKE_SOURCE_DIR}/porting/SDL2/lib/SDL2.lib"
    "${CMAKE_SOURCE_DIR}/porting/SDL2/lib/SDL2main.lib"
)

file(COPY "${CMAKE_SOURCE_DIR}/porting/SDL2/bin/SDL2.dll" DESTINATION "${CMAKE_BINARY_DIR}")

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
